/* main.c */
#include "app_inc.h"
#include "stdlib.h"

extern void NRF24L01_HW_GpioSetCEPin(bool enLogicOne);
extern void NRF24L01_HW_GpioSetCSPin(bool enLogicOne);
extern bool NRF24L01_HW_GpioGetIrqPin(void);

static int32_t do_show_help(int32_t argc, char *argv[]);
static int32_t do_set_cs_pin(int32_t argc, char *argv[]);
static int32_t do_set_ce_pin(int32_t argc, char *argv[]);
static int32_t do_get_irq_pin(int32_t argc, char *argv[]);

CMD_TableItem_T DemoCmdTable[] =
{
    {"help",   1, do_show_help},
    {"set_cs_pin", 2, do_set_cs_pin},
    {"set_ce_pin", 2, do_set_ce_pin},
    {"get_irq_pin", 1, do_get_irq_pin},
    {NULL}
};

int main(void)
{
    BSP_InitStdioUART(115200U);
    NRF24L01_HW_InitIO();

    printf("SPI_Nrf24l01Test Example ...\r\n");
    
    CMD_InstallHandler(&gCmdHandlerStruct);

    while (1)
    {
        CMD_LoopShell(DemoCmdTable);
    }
}

static int32_t do_show_help(int32_t argc, char *argv[])
{
    uint32_t idx = 0U;
    printf("# do_show_help()\r\n\r\n");
    while (DemoCmdTable[idx].CmdName != NULL)
    {
        printf(" - %s\r\n", DemoCmdTable[idx].CmdName);
        idx++;
    }
    printf("\r\n");
    return 0;
}

static int32_t do_set_ce_pin(int32_t argc, char *argv[])
{
    uint32_t val;

    val = atoi(argv[1]);
    switch (val)
    {
    case 0U:
        NRF24L01_HW_GpioSetCEPin(false);
        printf("CE Pin Low.\r\n");
        break;
    case 1U:
        NRF24L01_HW_GpioSetCEPin(true);
        printf("CE Pin High.\r\n");
        break;
    default:
        printf("Wrong setting value.\r\n");
    }
    return 0;
}

static int32_t do_set_cs_pin(int32_t argc, char *argv[])
{
    uint32_t val;

    val = atoi(argv[1]);
    switch (val)
    {
    case 0U:
        NRF24L01_HW_GpioSetCSPin(false);
        printf("CE Pin Low.\r\n");
        break;
    case 1U:
        NRF24L01_HW_GpioSetCSPin(true);
        printf("CE Pin High.\r\n");
        break;
    default:
        printf("Wrong setting value.\r\n");
    }
    return 0;
}

static int32_t do_get_irq_pin(int32_t argc, char *argv[])
{
    if (NRF24L01_HW_GpioGetIrqPin())
    {
        printf("IRQ Pin = 1\r\n");
    }
    else
    {
        printf("IRQ Pin = 0\r\n");
    }
    return 0;
}

/* EOF. */
